define([ "app", "jquery", "md5", "ecb", "services/UserService",
		"resources/UserResource", "resources/AddCompanyResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window", "$modalInstance", "$location", "$stateParams", "UserService", "UserResource", "AddCompanyResource" ];

			function controller($scope, $state, $window, $modalInstance, $location, $stateParams, UserService, UserResource, AddCompanyResource) {

				initDate(AddCompanyResource);
				
				$scope.setCompanyName =function(){
					var select =document.getElementById("companyCodeId");
					var index = select.selectedIndex;
					var companyName =select.options[index].label;
					$scope.companyVo.companyName=companyName;
				};
				$scope.saveCompany = function() {
					
					var regx = /^-?[1-9]*(\.\d*)?$|^-?0(\.\d*)?$/;

					
					// 判断是否为空
					if($scope.companyVo.singleCallPrice==null || $scope.companyVo.singleCallPriceCalled==null || $scope.companyVo.conferencePrice==null){
						return;
					}
					
					if(!regx.test($scope.companyVo.singleCallPrice)){
						return;
					}
					if(!regx.test($scope.companyVo.singleCallPriceCalled)){
						return;
					}

					if(!regx.test($scope.companyVo.conferencePrice)){
						return;
					}
					
					if ($scope.companyCreateForm.$invalid) {
						return;
					}
					
					
					$scope.companyCreateForm.$invalid = true;
					
					AddCompanyResource.save($scope.companyVo).success(function(data){
						$scope.companyCreateForm.$invalid = false;
						if (data.id) {
							$modalInstance.close($scope.companyVo);
						}else{
							alert("已经存在对应的企业");
							return;
						}
		            });
					
				};

				$scope.close = function($scope,AddCompanyResource) {
					$modalInstance.close();
				};
				
				/**
				 * 初始化下拉列表
				 */
				function initDate(AddCompanyResource){
					
					AddCompanyResource.queryOrgList(0).success(function(data){
						$scope.orgList=data;
		            });
					//$scope.selected=$scope.orgList[0].companyCode;
					//$scope.companyVo.companyCode=$scope.orgList[0].companyCode;
				}
			}
			controller.$inject = deps;
			return app.lazy.controller("AddCompanyController", controller);
		});
